//
//  MessageDialog.cpp
//  client
//
//  Created by DuanHongbo on 15/12/21.
//
//

#include "MessageDialog.h"

USING_NS_CC;
using namespace cocos2d::experimental;
using namespace cocos2d::ui;

bool MessageDialog::init(){
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    _isTwoButton = false;
    auto rootNode = CSLoader::createNode("ui2/With_choose.csb");
    if(rootNode){
        rootNode->setContentSize(Size(visibleSize.width,visibleSize.height));
        
        this->addChild(rootNode);
        cocos2d::ui::Helper::doLayout(rootNode);
        
        auto tipsNode = rootNode->getChildByName("tips");
        if(tipsNode){
            Text* messageLabel = dynamic_cast<Text*>(tipsNode->getChildByName("tips_txt"));
            if(messageLabel){
                messageLabel->setString("");
            }
            _messageLabel = messageLabel;
            
            _btnConfirm = dynamic_cast<Button*>(tipsNode->getChildByName("Button_queding_1"));
            _btnConfirm2 = dynamic_cast<Button*>(tipsNode->getChildByName("Button_queding"));
            _btnCancel2 = dynamic_cast<Button*>(tipsNode->getChildByName("Button_quxiao"));
          
            
            _confirmBtnLabel2 = dynamic_cast<Text*>(_btnConfirm2->getChildByName("name"));
            _cancelBtnLabel2 =  dynamic_cast<Text*>(_btnCancel2->getChildByName("name"));
            
            if(_btnConfirm){
                _btnConfirm->setVisible(true);
                _btnConfirm->addClickEventListener([this](Ref* sender){
                    if(this->_confirmCallback){
                        this->_confirmCallback(this);
                    }
                });
                _confirmLabel = dynamic_cast<Text*>(_btnConfirm->getChildByName("name"));
            }
            
            if(_btnConfirm2){
                _btnConfirm2->setVisible(false);
                _btnConfirm2->addClickEventListener([this](Ref* sender){
                    if(this->_confirmCallback){
                        this->_confirmCallback(this);
                    }
                });
            }
            
            if(_btnCancel2){
                _btnCancel2->setVisible(false);
                _btnCancel2->addClickEventListener([this](Ref* sender){
                    if(this->_cancelCallback){
                        this->_cancelCallback(this);
                    }
                });
            }
        }
        
        auto timeline = CSLoader::createTimeline("ui2/With_choose.csb");
        if(timeline){
            timeline->gotoFrameAndPlay(0,false);
            rootNode->runAction(timeline);
        }
    
        _rootNode = rootNode;
    }

    return true;
}

void MessageDialog::setMessage(const char* str)
{
    if(_messageLabel){
        _messageLabel->setString(str);
    }
}

void MessageDialog::setConfirmButton(const char* str)
{
    if(_isTwoButton){
        if(_confirmBtnLabel2){
            _confirmBtnLabel2->setString(str);
        }
    }else{
        if(_confirmLabel){
            _confirmLabel->setString(str);
        }
    }
}

void MessageDialog::setCancelButtonStr(const char* str)
{
    if(_isTwoButton){
        if(_cancelBtnLabel2){
            _cancelBtnLabel2->setString(str);
        }
    }
}

void MessageDialog::setConfirmCallback(const ButtonClickDelegate& delegate)
{
    _confirmCallback = delegate;
}

void MessageDialog::setCancelCallback(const ButtonClickDelegate& delegate)
{
    _cancelCallback = delegate;
}

void MessageDialog::setTwoButton(bool isTwoBtn){
    _isTwoButton = isTwoBtn;
    if(_isTwoButton){
        if(_btnConfirm){
            _btnConfirm->setVisible(false);
        }
        if(_btnConfirm2){
            _btnConfirm2->setVisible(true);
        }
        if(_btnCancel2){
            _btnCancel2->setVisible(true);
        }
        
    }else{
        if(_btnConfirm){
            _btnConfirm->setVisible(true);
        }
        if(_btnConfirm2){
            _btnConfirm2->setVisible(false);
        }
        if(_btnCancel2){
            _btnCancel2->setVisible(false);
        }
    }
}
